//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    imgUrl: baseJs.imgUrl,
    id: "",
    // 
    courseData: [],
    videoSrc: "",
    // 是否已经购买课程
    alreadyBuy: "",
    // 
    alertBuyFlag: true,
    // 输入课程兑换码
    inputCodeFlag: true,
    exchangeCode: "",
    // 付款
    payFlag: true,
    payType: '',
    orderNumber: "",
    // 输入支付密码
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
      id: options.id,
    })
    this.GetMyCourseList();
    this.CheckMyIsBuyCourse();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.videoContext = wx.createVideoContext('myVideo')
  },


  //
  GetMyCourseList: function() {
    var that = this;
    var id = this.data.id;
    var courseData = this.data.courseData;
    var videoSrc = this.data.videoSrc;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetCourseDetail&CourseId=" + id),
      success: function(res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        courseData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        if (courseData.chapterList.length == 0) {
          videoSrc = "";
        } else {
          videoSrc = courseData.chapterList[0].VideoPath;
        }
        that.setData({
          courseData: courseData,
          videoSrc: videoSrc
        })
        wx.setNavigationBarTitle({
          title: courseData.Title
        })
      }
    })
  },
  // 是否已经购买课程
  CheckMyIsBuyCourse: function() {
    var that = this;
    var id = this.data.id;
    var uid = this.data.uid;
    var alreadyBuy = this.data.alreadyBuy;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyIsBuyCourse&CourseId=" + id + "&UID=" + uid),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data))
        console.log(json);
        if (json.ResultFlag == 1) {
          if (json.ResultMsg == '已购买') {
            that.setData({
              alreadyBuy: '1',
            })
          } else {
            that.setData({
              alreadyBuy: '0',
            })
          }
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 课程列表点击
  courseTap: function(e) {
    console.log(e);
    this.setData({
      videoSrc: e.currentTarget.dataset.src,
      id: e.currentTarget.dataset.id,
    })
    this.GetMyCourseList();
    this.CheckMyIsBuyCourse();
  },
  // 播放
  bindPlay: function() {
    this.videoContext.play();
  },
  // 提示先购买
  alertGoBuy: function() {
    this.setData({
      alertBuyFlag: false,
    })
  },
  buyModalConfirm: function() {
    this.setData({
      alertBuyFlag: true,
    })
  },
  buyModalCancel: function() {
    this.setData({
      alertBuyFlag: true,
    })
  },
  // 
  actionSheetTap: function() {
    this.setData({
      sheetFlag: true
    })
  },
  // 点击使用兑换码
  useCodeTap: function() {
    this.setData({
      alertBuyFlag: true,
      inputCodeFlag: false,
    })
  },
  inputCode: function(e) {
    this.setData({
      exchangeCode: e.detail.value
    })
  },
  inputCodeConfirm: function() {
    var that = this;
    var uid = this.data.uid;
    // wx.request({
    //   url: baseJs.api + baseJs.encryUrl("method=GenerateCourseOrder&UID=" + uid),
    //   success: function (res) {
    //     var json = JSON.parse(baseJs.decryUrl(res.data))
    //     console.log(json);
        
    //   }
    // })
  },
  inputCodeCancel: function() {
    this.setData({
      inputCodeFlag: true,
    })
  },
  // 点击去支付
  useMoney: function() {
    this.setData({
      alertBuyFlag: true,
      payFlag: false
    })
  },
  // 取消付款
  cancelPay: function() {
    this.setData({
      payFlag: true
    })
  },
  //余额或微信 
  radioChange: function(e) {
    var payType = this.data.payType;
    payType = e.detail.value
    this.setData({
      payType: payType,
    });
  },
  // 付款
  goPay: function() {
    var that = this;
    var uid = this.data.uid;
    var payType = this.data.payType;
    var orderNumber = this.data.orderNumber;
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    if (payType == '') {
      wx.showToast({
        title: "请选择支付方式！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    this.setData({
      payFlag: true
    })
    if (payType == "1") {
      // 余额
      if (isAlreadyPayPassword == '1') {
        that.setData({
          showPayPwdInput: true,
          payFocus: true,
        })
      } else {
        wx.showToast({
          title: "请先去设置支付密码！",
          icon: 'none',
          duration: 2000
        });
      }
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=PayCourseOrder&OrderNumber=" + orderNumber + "&Type=2"),
        success: function(res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          console.log(json);
          if (json.ResultFlag == 1) {
            // wx.requestPayment({
            //   timeStamp: json.ResultData.timestamp,
            //   nonceStr: json.ResultData.nonce_str,
            //   package: 'prepay_id=' + json.ResultData.prepay_id,
            //   signType: 'MD5',
            //   paySign: json.ResultData.sign,
            //   success(res) {
            //     console.log(res);
            //   },
            //   fail(res) {
            //     console.log(res);
            //     wx.showToast({
            //       title: res.err_desc,
            //       icon: 'none',
            //       duration: 2000
            //     });
            //   }
            // })
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }

        }
      })
    }
  },
  // 输入支付密码
  //显示支付密码输入层
  showInputLayer: function() {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function() {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: ''
    });

  },
  // 获取焦点
  getFocus: function() {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function(e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //输入密码 确定 
  surePasswords: function() {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=PayCourseOrder&OrderNumber=" + orderNumber + "&Type=0"),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data))
              console.log(json);
              if (json.ResultFlag == "1") {
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 
})